home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 November / MACPOWER-1997-11.ISO.7z / MACPOWER-1997-11.ISO / Apple関連 / ResEdit 2.1.3 / Examples / CExamples / Source / XXXX.Edit.c < prev   
Text File  |  1994-09-14  |  9KB  |  305 lines

  1. /*
  2. File ResXXXXEd.c
  3.  
  4. Copyright Apple Computer, Inc. 1985-1990
  5. All rights reserved.
  6. */
  7.  
  8. #include    <types.h>
  9. #include    <memory.h>
  10. #include    <menus.h>
  11. #include    <resources.h>
  12.  
  13. #include    "ResEd.h"
  14.  
  15. #define        windowWidth            300
  16. #define        windowHeight        100
  17. #define        sizeOfXXXXResource    10
  18.  
  19. typedef struct rXXXXRec
  20.     {
  21.     ParentHandle    father;            /* Back ptr to dad */
  22.     Str255            name;            /* The name of this editor */
  23.     WindowPtr        wind;            /* This view's window */
  24.     Boolean            rebuild;        /* Set true if things have changed */
  25.     Boolean            resWasntLoaded;    /* TRUE if the resource should be released when the window is closed. */
  26.     unsigned char    windowType;
  27.     ResType            theResType;        /* Type of the resource beingedited. */
  28.     short            theResFile;        /* The home resfile of the window. */
  29.     short            codeResID;        /* Resource ID of the RSSC resource containing the picker or editor. */
  30.     Handle            hXXXX;            /* The resource we are working on */
  31.     } rXXXXRec;
  32.  
  33. typedef rXXXXRec    *rXXXXPtr;
  34. typedef rXXXXPtr    *rXXXXHandle;
  35.  
  36. /* Function prototypes. */
  37. pascal void DoMenu(short menu, short item, rXXXXHandle myXXXX);
  38.  
  39. /*- -    -  -  - -  -  -  - -  -  -    - -  -    -  - -    -  -  - -  -  -*/
  40.  
  41. /* Fix up the window name and title for our window. */
  42. void GetNameAndTitle(StringPtr windowTitle,StringPtr windowName,Handle thing)
  43.     {
  44.     strcpy(windowTitle,"¥pXXXX");
  45.     SetETitle(thing,windowTitle);
  46.     strncpy(windowName,windowTitle,*windowTitle + 1);    /* Add 1 for the length byte */
  47.     }
  48.  
  49. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  50.  
  51. pascal void EditBirth(Handle thing,ParentHandle dad)
  52.     {
  53.     rXXXXHandle        myXXXX;
  54.     WindowPtr        myWindow;
  55.     Str255            windowTitle,windowName;
  56.  
  57.     /* Prepare window title and request creation of a new window */
  58.     GetNameAndTitle(windowTitle, windowName, thing);
  59.     myWindow = EditorWindSetup(noDialog, noColor, windowWidth, windowHeight, windowTitle, windowName, true, ResEdID(), dad);
  60.  
  61.     /* If we got a new window, then start up the editor */
  62.     if (myWindow != nil)
  63.         {
  64.         /* This was called via a NEW, so make a new resource. */
  65.         if (GetHandleSize(thing) == 0L)
  66.             FixHand(sizeOfXXXXResource,thing);
  67.  
  68.         /* Get memory for and handle to our instance record */
  69.         myXXXX = (rXXXXHandle)NewHandle(sizeof(rXXXXRec));
  70.         if (MemError() != noErr)
  71.             {
  72.             CloseWindow(myWindow);
  73.             WindReturn(myWindow);                /* Mark the window record as being available */
  74.             return;
  75.             }
  76.  
  77.         /* Put information about this incarnation of the editor and the window it is */
  78.         /* serving into our record.(always passed around in the handle myXXXX).     */
  79.  
  80.         (*myXXXX)->father = dad;
  81.         strncpy((*myXXXX)->name, windowName, windowName[0] + 1);    /* Add 1 for the length byte */
  82.         (*myXXXX)->wind = myWindow;
  83.         (*myXXXX)->rebuild = false;
  84.         (*myXXXX)->resWasntLoaded = !WasItLoaded();
  85.         (*myXXXX)->windowType = editorWindow;
  86.         (*myXXXX)->theResType = 'XXXX';
  87.         (*myXXXX)->theResFile = HomeResFile(thing);
  88.         (*myXXXX)->codeResID = ResEdID();
  89.         (*myXXXX)->hXXXX = thing;
  90.                 
  91.         /* Let the main program know who is to manage this window by giving it both    */
  92.         /* our resource ID number and our instance record handle.        */
  93.         ((WindowPeek)myWindow)->refCon = (long)myXXXX;
  94.  
  95.         /* Set up any menus,views, etc. for this window here. */
  96.         }
  97.     }
  98.  
  99. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  100.  
  101. /* Not used for editors. */
  102. pascal void PickBirth(ResType t, ParentHandle dad)
  103.     {
  104. #pragma    unused (t, dad)
  105.     }
  106.  
  107. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  108.  
  109. pascal void DoEvent(EventRecord *evt, rXXXXHandle myXXXX)
  110.     {
  111.     Point        mousePoint;
  112.  
  113.     BubbleUp((Handle)myXXXX);        /* Move our item up in memory */
  114.     HLock((Handle)myXXXX);            /* Lock it down */
  115.  
  116.     /* Handle event passed to us by main program.  Just like a 'real' event loop, exceptノ    */
  117.     /* there is no loop and we don't have to handle as much because the main program    */
  118.     /* will do all the stuff that doesn't apply to us.                    */
  119.  
  120.     SetPort((*myXXXX)->wind);        /* Set the port to our window */
  121.  
  122.     switch (evt->what)
  123.         {
  124.         case mouseDown:
  125.             mousePoint = evt->where;    /* Point at which the event occured */
  126.             GlobalToLocal(&mousePoint);    /* Convert event location to local coords */
  127.             break;                        /* Do any special mouse down processing here. */
  128.     
  129.         case activateEvt:
  130.             if (evt->modifiers & activeFlag)
  131.                 {
  132.                 AbleMenu(editMenu, editNone);
  133.                 /* Do any activate processing here (such as inserting a menu) */
  134.                 }
  135.             else
  136.                 {
  137.                 /* Do any deactivate processing here (such as deleting a menu). */
  138.                 }
  139.             break;
  140.     
  141.         case updateEvt:
  142.             /* Do the appropriate update processing here.  Remember that BeginUpdate has already been called. */
  143.             PaintRect(&(*myXXXX)->wind->portRect);
  144.             break;
  145.     
  146.         case keyDown:
  147.             /* Do any key processing here. */
  148.             if ((evt->message & charCodeMask) == deleteKey)    /* Convert the delete character into a clear command. */
  149.                 {
  150.                 DoMenu(editMenu, clearItem, myXXXX);
  151.             }
  152.         break;
  153.  
  154.     case nullEvent:
  155.         /* Do any null event processing here (such as blinking a cursor).    */
  156.         break;
  157.         }
  158.     HUnlock((Handle)myXXXX);
  159.     }
  160.     
  161. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  162.  
  163. pascal void DoInfoUpdate(short oldID, short newID, rXXXXHandle myXXXX)
  164.     {
  165.     ParentHandle     father = (*myXXXX)->father;
  166.     Str255            windowTitle,windowName;
  167.  
  168.     HLock((Handle)myXXXX);
  169.     /* Since our ID has changed, we need to change our window title */
  170.     GetNameAndTitle(windowTitle, windowName, (Handle)(*myXXXX)->hXXXX);
  171.     GetWindowTitle(windowTitle, windowName,true, (*myXXXX)->father);
  172.  
  173.     /* Save the new name in my data structure. */
  174.     strncpy((*myXXXX)->name, windowName, windowName[0] + 1);    /* Add 1 for the length byte. */
  175.  
  176.     SetWTitle((*myXXXX)->wind, windowTitle);    /* Set the new window title. */
  177.  
  178.     /* Now, let our father object know that our ID has been changed */
  179.     (*father)->rebuild = true;            /* Rebuild the picker list. */
  180.     CallInfoUpdate(oldID, newID, (long)father, (*father)->wind->windowKind);
  181.     HUnlock((Handle)myXXXX);
  182.     }
  183.  
  184. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  185.  
  186. pascal Boolean IsThisYours(Handle thing, rXXXXHandle myXXXX)
  187.     {
  188.     return (thing == (Handle)(*myXXXX)->hXXXX);
  189.     }
  190.  
  191. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  192.  
  193. /*     Close down the window and get rid of any memory that has been allocated.
  194.     Returns TRUE if the operation was successful. If notRevert is false the resource isn't
  195.     released and the data handle isn't disposed (though all the handles it contains
  196.     are disposed).  */
  197. static Boolean DoClose(Boolean notRevert, rXXXXHandle myXXXX)
  198.     {
  199.     PassMenu (fileMenu, closeItem, (ParentHandle)myXXXX);
  200.     if (WasAborted())
  201.         {
  202.         return false;
  203.         }
  204.     else
  205.         {
  206.         CloseWindow((*myXXXX)->wind);
  207.         WindReturn((*myXXXX)->wind);    /* Mark the window record as being available */
  208.         SetTheCursor(arrowCursor);    /* Make sure the cursor is the arrow cursor */
  209.     
  210.         /* Delete any menus that we added and redraw the menu bar.    */
  211.         /* Be sure to dispose of any handles you are done with.        */
  212.         /* Release the resource if we were launched from a picker. */
  213.         if ((notRevert) && ((*myXXXX)->resWasntLoaded) && ((*((*myXXXX)->father))->windowType != editorWindow))
  214.             ReleaseResource((Handle)(*myXXXX)->hXXXX);    /* Let it be free (if it is not changed)! */
  215.         
  216.         if (notRevert)
  217.             DisposHandle((Handle)myXXXX);
  218.         return true;
  219.         }
  220.     }
  221.  
  222. /*- -  -  -    - -  -    -  - -    -  -  - -  -  -  - -  -  -    - -  -    -*/
  223.  
  224. pascal void DoMenu(short menu, short item, rXXXXHandle myXXXX)
  225.     {
  226.  
  227.     BubbleUp((Handle)myXXXX);
  228.     HLock((Handle)myXXXX);
  229.     SetPort((*myXXXX)->wind);                    /* Set the port to our window */
  230.  
  231.     /* Again, we handle the menu stuff just as we would in a 'real' application    */
  232.     /* except that we only have to handle those items that apply to our editor.    */
  233.         
  234.     switch (menu)
  235.         {
  236.         case fileMenu:
  237.             switch (item)
  238.                 {
  239.                 case closeItem:
  240.                     if (DoClose(true, myXXXX))    /* Close our window                        */
  241.                         {
  242.                         return;                    /* Return immediately since our resource is gone! */
  243.                         }
  244.                     break;
  245.  
  246.                 case saveItem:                /* Pass the save on to other windows.    */
  247.                     PassMenu(fileMenu,saveItem, (ParentHandle)myXXXX);
  248.                     break;
  249.  
  250.                 case printItem:
  251.                     PrintWindow(nil);
  252.                     break;
  253.                 }
  254.             break;
  255.         case rsrcMenu:
  256.             switch(item)
  257.                 {
  258.                 case rsrcRevertItem:
  259.                     if (NeedToRevert((*myXXXX)->wind, (Handle)(*myXXXX)->hXXXX))
  260.                         {
  261.                         /* The area under the window will need to be updated     */
  262.                         InvalRect(&(*myXXXX)->wind->portRect);
  263.         
  264.                         /* Read in the old copy from disk (see documentation for revertResource)    */
  265.                         /* Clear it out unless this was a newly created resource    */
  266.                         if (!RevertThisResource((ParentHandle)myXXXX, (Handle)(*myXXXX)->hXXXX))
  267.                             {
  268.                             /* The resource was newly added so we need to remove it.*/
  269.         
  270.                             /* Make sure that the picker list is rebuilt to remove this item.                                         */
  271.                             (*((*myXXXX)->father))->rebuild = true;
  272.         
  273.                             if (DoClose(false, myXXXX))    /* Close the window.            */
  274.                                 {
  275.                                 RERemoveAnyResource((*myXXXX)->theResFile, (*myXXXX)->hXXXX);    /* Dispose the resource itself. */
  276.                                 DisposHandle((Handle)myXXXX);
  277.                                 return;                    /* Since the resource is gone.    */
  278.                                 }
  279.                             }
  280.                         }
  281.                     break;
  282.  
  283.                 case rsrcGetInfoItem:        /* Show GetInfo window */
  284.                     ShowInfo((Handle)(*myXXXX)->hXXXX,(ParentHandle)myXXXX);
  285.                     break;
  286.                 }
  287.             break;
  288.         case editMenu:            /* Implement the edit menu here    */
  289.             switch (item)
  290.                 {
  291.                 case cutItem:
  292.                     break;
  293.                 case copyItem:
  294.                     break;
  295.                 case pasteItem:
  296.                     break;
  297.                 case clearItem:
  298.                     break;
  299.                 }
  300.             break;
  301.         }
  302.     HUnlock((Handle)myXXXX);
  303.     }
  304.  
  305.